<template>
  <div class="user">
    <div class="bread">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main-content">
      <!--按钮-->
      <div class="btn_content">
        <div>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true"> 新增分类</el-button>
        </div>
      </div>
      <!--table表单-->
      <div class="table_content">
        <div>
          <el-table :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"  border style="width: 100%">
            <el-table-column prop="id" label="分类编号" align="center"  width="200"></el-table-column>
            <el-table-column prop="className" label="类别名称" align="center"  width="250"></el-table-column>
            <el-table-column prop="classDes" label="类别描述" align="center"  width="300px"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
                <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--分页按钮-->
      <div class="page_content">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="tableData.length">
        </el-pagination>
      </div>
    </div>
    <!--添加页面-->
    <el-dialog title="输入信息" :visible.sync="dialogFormVisible">
      <el-form :model="Classity">
        <el-form-item label="类别名称：" :label-width="formLabelWidth">
          <el-input v-model="Classity.className" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别描述：" :label-width="formLabelWidth">
          <el-input v-model="Classity.classDes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNews()">添 加</el-button>
      </div>
    </el-dialog>
    <!--修改信息界面-->
    <el-dialog  title="修改类别" :visible.sync="updateFormVisible">
      <el-form :model="Inform">
        <el-form-item label="类别名称：" :label-width="formLabelWidth">
          <el-input v-model="Inform.className" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别描述：" :label-width="formLabelWidth">
          <el-input v-model="Inform.classDes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateNews()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "User",
    data() {
      return {
        options:[{
          value:'1',
          label:'管理员',
        },{
          value:'2',
          label:'普通用户',
        }],
        dialogFormVisible: false,//添加页面
        updateFormVisible: false,//修改页面
        formLabelWidth: '100px',
        currentPage: 1, //初始页
        pageSize: 5,    //每页的数据
        Classity: {},  //添加中转站
        Inform: {},//查询的单个存储
        tableData: [{}],
      }
    },
    methods:{
      // 初始页currentPage、初始每页数据数pagesize和数据data
      handleSizeChange: function (size) {
        this.pagesize = size;
        console.log(this.pagesize)  //每页下拉显示数据
      },
      handleCurrentChange: function (currentPage) {
        this.currentPage = currentPage;
        console.log(this.currentPage)  //点击第几页
      },
      /*查询所有分类*/
      findAll() {
        let _this = this
        this.$axios.get("http://localhost:8066/classity/getClassify").then(function (res) {
          _this.tableData = res.data
          console.log(_this.tableData)
        }).catch(function (err) {
          alert('查询失败')
        })
      },
      /*添加分类*/
      addNews() {
        if (!this.Classity.className) {alert('请输入分类名称');return false}
        if (!this.Classity.classDes) {alert('请输入分类描述');return false}
        console.log(this.Role)
        let _this = this
        this.$axios.post("http://localhost:8066/classity/setClassify", this.Classity).then(function () {
          //清空本次保存数据
          _this.$message({
            showClose: true,
            message: '角色信息添加成功!',
            type: 'success'
          });
          _this.Role = {};
          _this.dialogFormVisible = false
          _this.findAll();
        }).catch(function (err) {
          _this.$message({
            showClose: true,
            message: '角色信息添加失败!',
            type: 'error'
          });
        })
      },
      /*根据id查询分类*/
      handleEdit(id) {
        this.updateFormVisible = true
        let _this = this
        this.$axios.get("http://localhost:8066/classity/getClassifyById/" + id).then(function (res) {
          _this.Inform = res.data
          console.log(_this.Inform)
        }).catch(function (err) {
          alert('查询失败')
        })
      },
      /*更新分类*/
      updateNews() {
        let _this = this
        // 发送axios请求，根据查询一个
        this.$axios.post("http://localhost:8066/classity/updateClassify", _this.Inform).then(function (res) {
          _this.$message({
            showClose: true,
            message: '类别信息修改成功!',
            type: 'success'
          });
          _this.updateFormVisible = false
          _this.findAll();
        }).catch(function (err) {
          _this.$message({
            showClose: true,
            message: '角色信息修改失败!请联系管理员！',
            type: 'error'
          });
        })
      },
      /*删除分类*/
      handleDelete(id) {
        let _this = this
        if (window.confirm('确定要删除分类吗？')) {
          // 发送axios请求，根据查询一个
          this.$axios.post("http://localhost:8066/classity/deletedById/" + id).then(function () {
            _this.findAll();
            _this.$message({
              showClose: true,
              message: '类别删除成功!',
              type: 'success'
            });
          }).catch(function (err) {
            _this.$message({
              showClose: true,
              message: '类别删除失败!请联系管理员！',
              type: 'error'
            });
          })
        }
      },
    },
    mounted() {
      this.findAll()
    }
  }
</script>

<style scoped>
  .main-content{
    padding: 10px;
    margin-top: 25px;
    height: 530px;
    width: 1180px;
    background-color: white;
  }
  .btn_content{
    width: 100%;
    height: 50px;
    /*background-color: red;*/
  }
  .table_content{
    width: 100%;
    height: 400px;
    /*background-color: cornflowerblue;*/
  }
  .page_content{
    padding-top: 10px;
    width: 100%;
    height: 60px;
    /*background-color: #6f6f6f;*/
  }
</style>
